Cipher Object
Cipher வகுப்பு Node.js இன் crypto தொகுதியின் ஒரு பகுதியாகும். இது பல்வேறு அல்காரிதம்களைப் பயன்படுத்தி தரவை குறியாக்கம் செய்ய ஒரு வழியை வழங்குகிறது. Cipher நிகழ்வுகள் crypto.createCipheriv() முறையைப் பயன்படுத்தி உருவாக்கப்படுகின்றன.
குறிப்பு:
crypto.createCipher() முறை பாதுகாப்பு கவலைகள் காரணமாக Node.js v10.0.0 முதல் மறக்கப்பட்டது. எப்போதும் crypto.createCipheriv() ஐப் பயன்படுத்தவும், இதற்கு வெளிப்படையான துவக்க திசையன் (IV) தேவைப்படுகிறது.
Import Crypto Module
// Import the crypto module
const crypto = require('crypto');
// Create a cipher with createCipheriv
const algorithm = 'aes-256-cbc';
const key = crypto.randomBytes(32); // 32 bytes for AES-256
const iv = crypto.randomBytes(16); // 16 bytes for AES
const cipher = crypto.createCipheriv(algorithm, key, iv);
Cipher Methods
| முறை | விளக்கம் |
|---|---|
| cipher.update(data[, inputEncoding][, outputEncoding]) | தரவுடன் சைபரைப் புதுப்பிக்கிறது. inputEncoding வழங்கப்பட்டால், தரவு குறிப்பிட்ட குறியீட்டைப் பயன்படுத்தி ஒரு சரமாகும். outputEncoding குறிப்பிடப்பட்டால், திரும்பிய மதிப்பு குறிப்பிட்ட குறியீட்டைப் பயன்படுத்தி ஒரு சரமாக இருக்கும். இல்லையெனில், ஒரு Buffer திரும்பும். |
| cipher.final([outputEncoding]) | மீதமுள்ள எந்தவொரு குறியாக்கப்பட்ட உள்ளடக்கங்களையும் திரும்பப் பெறுகிறது. outputEncoding குறிப்பிடப்பட்டால், ஒரு சரம் திரும்பும்; இல்லையெனில், ஒரு Buffer திரும்பும். |
| cipher.setAAD(buffer[, options]) | ஒரு AEAD அல்காரிதத்தைப் (GCM அல்லது CCM போன்றவை) பயன்படுத்தும் போது, கூடுதல் அங்கீகரிக்கப்பட்ட தரவை (AAD) அமைக்கிறது. |
| cipher.getAuthTag() | ஒரு AEAD அல்காரிதத்தைப் பயன்படுத்தும் போது, இந்த முறை அங்கீகார டேக்கைக் கொண்டுள்ள ஒரு Buffer ஐத் திரும்பப் பெறுகிறது. |
| cipher.setAutoPadding([autoPadding]) | autoPadding உண்மையாக இருக்கும் போது (இயல்புநிலை), padding பயன்படுத்தப்படும். தரவு கைமுறையாக padding செய்யப்பட்டால் முடக்கவும். |
Basic Encryption Example
பின்வரும் எடுத்துக்காட்டு AES-256-CBC அல்காரிதத்தைப் பயன்படுத்தி தரவை எவ்வாறு குறியாக்கம் செய்வது என்பதை விளக்குகிறது:
const crypto = require('crypto');
// Generate encryption key and initialization vector
// In a real application, you would securely store and retrieve these values
const key = crypto.randomBytes(32); // Key for AES-256 (32 bytes)
const iv = crypto.randomBytes(16); // IV for AES (16 bytes)
// Create a cipher
const algorithm = 'aes-256-cbc';
const cipher = crypto.createCipheriv(algorithm, key, iv);
// Data to encrypt
const plainText = 'This is a secret message';
// Encrypt the data
let encrypted = cipher.update(plainText, 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log('Original Text:', plainText);
console.log('Encrypted Text:', encrypted);
console.log('Key (hex):', key.toString('hex'));
console.log('IV (hex):', iv.toString('hex'));
// The encrypted message, key, and IV would be needed for decryption
Encrypting with Different Algorithms
Node.js பல குறியாக்க அல்காரிதம்களை ஆதரிக்கிறது. வெவ்வேறு அல்காரிதம்களை எவ்வாறு பயன்படுத்துவது என்பது இங்கே:
const crypto = require('crypto');
// The data to encrypt
const plainText = 'Hello, this is a test message';
// Function to encrypt data with different algorithms
function encryptWithAlgorithm(algorithm, keySize, ivSize, plainText) {
// Generate key and IV
const key = crypto.randomBytes(keySize);
const iv = crypto.randomBytes(ivSize);
// Create cipher
const cipher = crypto.createCipheriv(algorithm, key, iv);
// Encrypt data
let encrypted = cipher.update(plainText, 'utf8', 'hex');
encrypted += cipher.final('hex');
return {
algorithm,
encrypted,
key: key.toString('hex'),
iv: iv.toString('hex')
};
}
// Test different algorithms
const algorithms = [
{ name: 'aes-128-cbc', keySize: 16, ivSize: 16 },
{ name: 'aes-192-cbc', keySize: 24, ivSize: 16 },
{ name: 'aes-256-cbc', keySize: 32, ivSize: 16 },
{ name: 'aes-256-gcm', keySize: 32, ivSize: 16 }
];
algorithms.forEach(algo => {
try {
const result = encryptWithAlgorithm(algo.name, algo.keySize, algo.ivSize, plainText);
console.log(`Encrypted with ${result.algorithm}: ${result.encrypted}`);
} catch (error) {
console.error(`Error with ${algo.name}: ${error.message}`);
}
});
Encrypting Binary Data
உரையுடன் கூடுதலாக பைனரி தரவையும் நீங்கள் குறியாக்கம் செய்யலாம்:
const crypto = require('crypto');
const fs = require('fs');
// Generate key and IV
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
// Create read and write streams
const readStream = fs.createReadStream('input.jpg');
const writeStream = fs.createWriteStream('encrypted.jpg.enc');
// Create cipher stream
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
// Encrypt the file
readStream
.pipe(cipher)
.pipe(writeStream);
// Save the key and IV for decryption
fs.writeFileSync('encryption_key.txt', key.toString('hex'));
fs.writeFileSync('encryption_iv.txt', iv.toString('hex'));
writeStream.on('finish', () => {
console.log('File encryption completed');
});
Using AEAD Encryption
தொடர்புடைய தரவுடன் அங்கீகரிக்கப்பட்ட குறியாக்கம் (AEAD) இரண்டும் இரகசியத்தன்மை மற்றும் தரவு ஒருமைப்பாட்டை வழங்குகிறது:
const crypto = require('crypto');
// Data to encrypt
const plainText = 'Secret message';
const associatedData = 'Additional data to authenticate';
// Generate key and IV (nonce)
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(12); // 12 bytes (96 bits) is recommended for GCM
// Create cipher using AES-GCM (an AEAD algorithm)
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
// Set the Additional Authenticated Data (AAD)
cipher.setAAD(Buffer.from(associatedData));
// Encrypt the data
let encrypted = cipher.update(plainText, 'utf8', 'hex');
encrypted += cipher.final('hex');
// Get the authentication tag
const authTag = cipher.getAuthTag();
console.log('Encrypted Text:', encrypted);
console.log('Auth Tag (hex):', authTag.toString('hex'));
console.log('Key (hex):', key.toString('hex'));
console.log('IV (hex):', iv.toString('hex'));
console.log('Associated Data:', associatedData);
// All this information is needed for decryption and verification
Manual Padding Control
நீங்கள் கைமுறையாக padding நடத்தையைக் கட்டுப்படுத்தலாம்:
const crypto = require('crypto');
// Generate key and IV
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
// Data to encrypt
const plainText = 'This is a test message';
// Function to encrypt with different padding options
function encryptWithPadding(usePadding) {
// Create cipher
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
// Set padding option
cipher.setAutoPadding(usePadding);
try {
// Encrypt data
let encrypted = cipher.update(plainText, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
} catch (error) {
return `Error: ${error.message}`;
}
}
// With default padding (true)
console.log('With padding:', encryptWithPadding(true));
// Without padding
// This will likely fail unless data length is a multiple of the block size
console.log('Without padding:', encryptWithPadding(false));
// Example with manual padding to block size (16 bytes for AES)
function manualPadding(text) {
const blockSize = 16;
const padLength = blockSize - (text.length % blockSize);
return text + '\0'.repeat(padLength);
}
// Create cipher without auto padding
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
cipher.setAutoPadding(false);
// Manually pad the data
const paddedText = manualPadding(plainText);
console.log('Original length:', plainText.length);
console.log('Padded length:', paddedText.length);
// Encrypt manually padded data
let encrypted = cipher.update(paddedText, 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log('With manual padding:', encrypted);
Complete Encryption/Decryption Example
குறியாக்கம் மற்றும் குறிவிலக்கம் இரண்டையும் காட்டும் முழுமையான எடுத்துக்காட்டு இங்கே:
const crypto = require('crypto');
// The message to encrypt
const message = 'This is a secret message that needs to be encrypted';
// Generate encryption key and IV
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
// Encryption function
function encrypt(text) {
// Create cipher
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
// Encrypt data
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
// Decryption function (using the Decipher class)
function decrypt(encryptedText) {
// Create decipher with the same key and IV
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
// Decrypt data
let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
// Encrypt the message
const encryptedMessage = encrypt(message);
console.log('Original Message:', message);
console.log('Encrypted Message:', encryptedMessage);
// Decrypt the message
const decryptedMessage = decrypt(encryptedMessage);
console.log('Decrypted Message:', decryptedMessage);
// Verify the result
console.log('Decryption successful:', message === decryptedMessage);
Encryption with a Password
பல பயன்பாடுகளுக்கு, நீங்கள் ஒரு கடவுச்சொல்லிலிருந்து குறியாக்க விசையைப் பெற விரும்பலாம்:
const crypto = require('crypto');
// Password and salt
const password = 'mysecretpassword';
const salt = crypto.randomBytes(16);
// Generate a key from the password
function getKeyFromPassword(password, salt) {
// Use PBKDF2 to derive a key from the password
return crypto.pbkdf2Sync(password, salt, 100000, 32, 'sha256');
}
// Password-based encryption
function encryptWithPassword(text, password) {
// Generate key from password
const key = getKeyFromPassword(password, salt);
// Generate IV
const iv = crypto.randomBytes(16);
// Create cipher
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
// Encrypt data
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
// Return encrypted data and IV (we'll need both for decryption)
return {
iv: iv.toString('hex'),
salt: salt.toString('hex'),
encryptedData: encrypted
};
}
// Password-based decryption
function decryptWithPassword(encryptedInfo, password) {
// Get the key from the password
const key = getKeyFromPassword(
password,
Buffer.from(encryptedInfo.salt, 'hex')
);
// Get the IV from encryptedInfo
const iv = Buffer.from(encryptedInfo.iv, 'hex');
// Create decipher
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
// Decrypt data
let decrypted = decipher.update(encryptedInfo.encryptedData, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
// Test encryption with password
const message = 'Secret message protected by a password';
const encryptedInfo = encryptWithPassword(message, password);
console.log('Encrypted:', encryptedInfo);
// Test decryption with password
const decryptedMessage = decryptWithPassword(encryptedInfo, password);
console.log('Decrypted:', decryptedMessage);
// Try with wrong password
try {
const wrongPassword = 'wrongpassword';
const failedDecryption = decryptWithPassword(encryptedInfo, wrongPassword);
console.log('Decrypted with wrong password:', failedDecryption);
} catch (error) {
console.log('Decryption failed with wrong password:', error.message);
}
Supported Encryption Algorithms
Node.js பல குறியாக்க அல்காரிதம்களை ஆதரிக்கிறது. நீங்கள் ஆதரிக்கப்படும் அனைத்து அல்காரிதம்களின் பட்டியலைப் பெறலாம்:
const crypto = require('crypto');
// Get all supported cipher algorithms
console.log(crypto.getCiphers());
பொதுவான அல்காரிதம்கள் அடங்கும்:
| அல்காரிதம் | விசை அளவு (bytes) | IV அளவு (bytes) | விளக்கம் |
|---|---|---|---|
| aes-128-cbc | 16 | 16 | CBC பயன்முறையில் 128-பிட் விசையுடன் AES |
| aes-192-cbc | 24 | 16 | CBC பயன்முறையில் 192-பிட் விசையுடன் AES |
| aes-256-cbc | 32 | 16 | CBC பயன்முறையில் 256-பிட் விசையுடன் AES |
| aes-128-gcm | 16 | 12 | GCM பயன்முறையில் 128-பிட் விசையுடன் AES (AEAD) |
| aes-256-gcm | 32 | 12 | GCM பயன்முறையில் 256-பிட் விசையுடன் AES (AEAD) |
| chacha20-poly1305 | 32 | 12 | ChaCha20-Poly1305 (AEAD) |